package org.esprit.ocm.client.model.ec2.datasource;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.model.ec2.GroupRecord;
import org.esprit.ocm.dto.ec2.impl.GroupDescriptionMappingDto;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class GroupLocalDS extends DataSource {

	private static GroupLocalDS	instance	= null;

	public static GroupLocalDS getInstance(List<GroupDescriptionMappingDto> groups) {
//		if (instance == null) {
			instance = new GroupLocalDS("groupLocalDS", groups);
//		}
		return instance;
	}

	public GroupLocalDS(String id, List<GroupDescriptionMappingDto> groups) {

//		setID(id);

		DataSourceTextField nameField = new DataSourceTextField("name", "name");
		DataSourceTextField descriptionField = new DataSourceTextField("description", "description");
		DataSourceTextField ownerField = new DataSourceTextField("owner", "owner");
		
		setFields(nameField, descriptionField, ownerField);

		List<ListGridRecord> l = new ArrayList<ListGridRecord>();
		for (GroupDescriptionMappingDto from : groups) {

			GroupRecord to = new GroupRecord();
			
			to.setName(from.getName());
			to.setDescription(from.getDesc());
			to.setOwner(from.getOwner());
			
			l.add(to);
		}

		setTestData(l.toArray(new GroupRecord[0]));
		setClientOnly(true);
	}

}
